<?php
require($_SERVER['DOCUMENT_ROOT'] . '/setting.php');
function verifyToken($token)
{
    global $redis_pz;
    $redis = new Redis();
    $redis->connect($redis_pz['host'], $redis_pz['port']);
    $redis->auth($redis_pz['password']);
    $data = $redis->get($token);
    if ($data) {
        $data = json_decode($data, true);
        if ($data['timestamp'] > time()) {
            return true;
        }
    }
    return false;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'error: requests are not allowed']);
    exit;
}
require($_SERVER['DOCUMENT_ROOT'] . '/cofd/functions.php');
$data_y = file_get_contents("php://input");
$data_x = json_decode($data_y, true);
$token  = $data_x['token'];
if ($token != '') {
    $token  = encrypt($token, 'D', 'yhsdylpo9342');
} else {
    echo json_encode(['success' => false, 'message' => 'error: invalid token']);
    exit;
}
if (!verifyToken($token)) {
    echo json_encode(['success' => false, 'message' => 'error: invalid token']);
    exit;
}
function gbToMb($gb) {
    $mb = $gb * 1024;
    return $mb;
}
require($_SERVER['DOCUMENT_ROOT'] . '/cofd/common.php');
$user_id = $data_x['userid'];
$sql = "SELECT id,names,timei,pwd,asets,numd,sizes FROM `files` WHERE `froms` = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $user_id);
if($stmt->execute()) {
    $result = $stmt->get_result();
    $list_s = [];
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $row['sizes'] = gbToMb($row['sizes']);
            $row['id'] = encrypt($row['id'], 'E', 'yhsdylpo9342');
            $list_s[] = $row;
        }
        echo json_encode(['success' => true, 'message' => 'success', 'data' => $list_s]);
    }else{
        echo json_encode(['success' => false, 'message' => 'error: database error']);
    }
}else{
    echo json_encode(['success' => false, 'message' => 'error: database error']);
}
$stmt->close();
$conn->close();
